package com.glory.brepda.activity.recipe;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.RecipeInfoAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.RecipeParaInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RecipeInfoQueryActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.etbdcode)
    FreeUI_ClearEditText etbdcode;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvParaList)
    RecyclerView rvParaList;

    private List<RecipeParaInfoEntity.RECIPELISTBean> paraInfoList = new ArrayList<>();
    private RecipeInfoAdapter paraAdapter;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_para_info;
    }



    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("程序参数查询");
        tvBaseSubmit.setText("查询");
        initRecyclerView(rvParaList,this);
        paraAdapter = new RecipeInfoAdapter(null,false);
        paraAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        paraAdapter.isFirstOnly(true);
        rvParaList.setAdapter(paraAdapter);
    }

    @Override
    protected void initData() {
        initEdit(etLotId, this::submit);
    }

    private void submit(String etLotId) {
        FreeUI_EntityCallBack<BaseResponseBean<RecipeParaInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<RecipeParaInfoEntity>>
                (new TypeToken<BaseResponseBean<RecipeParaInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<RecipeParaInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    RecipeParaInfoEntity body = response.body().getResponse().getBody();
                    if(body.getRECIPELIST() !=null){
                        for (RecipeParaInfoEntity.RECIPELISTBean entity : body.getRECIPELIST()){
                            paraInfoList.add(entity);
                        }
                    }
                    paraAdapter.setNewData(paraInfoList);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<RecipeParaInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "BRERECIPEPARAQUERY");
            }
        };

        paraInfoList.clear();
        JsonObject map = new JsonObject();
        map.addProperty("LOTID",etLotId);
        map.addProperty("BDCODE",getText(etbdcode));
        NetUtils.requestPostNet("IF.BRERECIPEPARAQUERY","BRERECIPEPARAQUERY", RecipeInfoQueryActivity.this,map,callBack);
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                submit(getText(etLotId));
        }
    }
}
